define(function(require) {

    'use strict';

    var Backbone = require('backbone'),
        Base = require('components/pop/base'),
        template = require('text!components/pop/templates/normal_reply.html'),
        WbEditor = require('components/wb_editor_v2/com'),
        utils = require('core/utils');

    return Base.extend({

        template: _.template(template),
        className: function() {
            return this.options.className;
        },

        events: {
            'click .js-close': 'hide',
            'click .js-btn-confirm': 'triggerCallback'
        },

        initialize: function(options) {
            Base.prototype.initialize.call(this, options);
            this.data = options.data;
        },

        render: function() {
            var view = this;

            view.$el.html(view.template(view.data));

            // if (!view.wbEditor) {
            view.wbEditorEle = view.$el.find('.wb-sender__input');

            view.wbEditor = new WbEditor({
                config: {
                    modules: ['emotion', 'hyperlink'],
                    data: {
                        type: 'text',
                        content: view.data.content
                    }
                },
                el: view.wbEditorEle
            });
            // };
            return view;
        },

        positioning: function() {
            var view = this;
            var className = view.options.className;

            view.$el.show();
            if (className.indexOf('left') !== -1) {
                view.$el.position({
                    of: view.target,
                    my: 'right center',
                    at: 'left center',
                    // collision: 'none'
                });
            } else {
                view.$el.position({
                    of: view.target,
                    my: 'left center',
                    at: 'right center',
                    // collision: 'none'
                });
            }

            view.el.className = view.options.className;
        },

        setData: function(data) {
            var view = this;

            var obj = {
                data: {
                    type: data.type,
                    content: data.content
                }
            };
            view.wbEditor.updateContent(obj);
        },

        reset: function(obj) {
            var callback = obj.callback,
                target = obj.target,
                trigger = obj.trigger,
                data = obj.data,
                className = obj.className;

            this.options.className = className;
            this.setCallback(callback);
            this.setTarget(target);
            this.setTrigger(trigger);
            this.setData(data);
            this.positioning();
            this.show();
        },

        triggerCallback: function() {
            var view = this;

            var obj = view.wbEditor.getContent();
            if (!obj.content) {
                view.wbEditor.focus();
                return false;
            }

            // view.setData(obj);
            view.callback.call(view, obj);
            view.hide();
            view.wbEditor.clear();
        }
    });
});
